package com.example.smallmall.controller;

import com.example.smallmall.common.api.CommonResult;
import com.example.smallmall.component.WeatherManager;
import com.example.smallmall.dto.LiveWeather;
import com.example.smallmall.dto.WeatherDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

@Api(tags = "第三方接口")
@RestController
@RequestMapping(value = "/Api")
public class ApiController {
    final WeatherManager weatherManager;

    public ApiController(WeatherManager weatherManager) {
        this.weatherManager = weatherManager;
    }

    //用来校验传参是否正确
    @Value("${city.code}")
    private String cityCode;

    @ApiOperation("获取当前城市天气")
    @RequestMapping(value = "city/{id:1[0-9]{8}]}", method = RequestMethod.GET)
    @ResponseBody
    public WeatherDto getWeather(@PathVariable("id") String id) {
        String vliCode = String.format(",%s,", id);
        if (!cityCode.contains(vliCode)) {
            throw new RuntimeException("no_city_id");
        }
        return weatherManager.getById(id);
    }

    @ApiOperation("获取当前城市实况天气")
    @RequestMapping(value = "getLiveWeather", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<LiveWeather> getLiveWeather() {
        return CommonResult.success(weatherManager.getWeather());
    }
}
